<?php
use AlibabaCloud\SDK\Dypnsapi\V20170525\Dypnsapi;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dypnsapi\V20170525\Models\GetMobileRequest;
use AlibabaCloud\SDK\Dypnsapi\V20170525\Models\GetSmsAuthTokensRequest;
use AlibabaCloud\SDK\Dypnsapi\V20170525\Models\VerifySmsCodeRequest;

/**
 * 阿里云一键登录
 */
class Mobile {

    protected static $accessKeyId = 'LTAI5tE6FUVonK1SJJJXH38S';
    protected static $accessKeySecret = '1IYnDjjQab4KZPVFZI6fUgWnP3Ma8i';
    protected static $smsTemplateCode = 'SMS_139870005';

    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Dypnsapi
     */
    public static function createClient($accessKeyId, $accessKeySecret){
        $config = new Config([]);
        $config->accessKeyId = $accessKeyId;
        $config->accessKeySecret = $accessKeySecret;
        return new Dypnsapi($config);
    }


    public static function getMobile($accessToken){
        $client = self::createClient(self::$accessKeyId, self::$accessKeySecret);
        try {
            $request = new GetMobileRequest([]);
            $request->accessToken = $accessToken;
            $response = $client->getMobile($request);
            $code = $response->body->code;
            if (!Utils::equalString($code, "OK")) {
                //var_dump("错误信息:" . $response->body->message . "");
                return ['code' => 0, 'msg' => "错误信息:" . $response->body->message . "", 'data' => null];
            } else {
                //var_dump("响应结果:" . (string) ($response->body) . "");
                return ['code' => 1, 'msg' => "ok", 'data' => $response->body->getMobileResultDTO];
            }
        }catch (Exception $e) {
            return ['code' => 0, 'msg' => "错误信息:" . $e->getMessage() . "", 'data' => null];
        }
    }

    public static function getSmsAuthToke($packageName,$signName,$sceneCode,$osType,$BundleId){
        $client = self::createClient(self::$accessKeyId, self::$accessKeySecret);
        try {
            $request = new GetSmsAuthTokensRequest([]);
            $request->packageName = $packageName;
            $request->signName = $signName;
            $request->sceneCode = $sceneCode;
            $request->osType = $osType;
            $request->bundleId = $BundleId;
            $request->smsTemplateCode = self::$smsTemplateCode;
            $request->expire = 7200;
            $response = $client->getSmsAuthTokens($request);
            $code = $response->body->code;
            if (!Utils::equalString($code, "OK")) {
                //var_dump("错误信息:" . $response->body->message . "");
                return ['code' => 0, 'msg' => "错误信息:" . $response->body->message . "", 'data' => null];
            } else {
                //var_dump("响应结果:" . (string) ($response->body) . "");
                return ['code' => 1, 'msg' => "ok", 'data' => $response->body->data];
            }
        }catch (Exception $e) {
            return ['code' => 0, 'msg' => "错误信息:" . $e->getMessage() . "", 'data' => null];
        }
    }

    public static function verifySmsCode($smsCode,$phoneNumber,$smsToken){
        $client = self::createClient(self::$accessKeyId, self::$accessKeySecret);
        try {
            $request = new VerifySmsCodeRequest([]);
            $request->smsCode = $smsCode;
            $request->phoneNumber = $phoneNumber;
            $request->smsToken = $smsToken;
            $response = $client->verifySmsCode($request);
            $code = $response->body->code;
            if (!Utils::equalString($code, "OK")) {
                //var_dump("错误信息:" . $response->body->message . "");
                return ['code' => 0, 'msg' => "错误信息:" . $response->body->message . "", 'data' => null];
            } else {
                //var_dump("响应结果:" . (string) ($response->body) . "");
                return ['code' => 1, 'msg' => "ok", 'data' => $response->body->data];
            }
        }catch (Exception $e) {
            return ['code' => 0, 'msg' => "错误信息:" . $e->getMessage() . "", 'data' => null];
        }
    }
}
